<!--
 * @Description: 柱状图
 * @Version: 1.0
 * @Autor: gaoluo
 * @Date: 2021-09-09 10:01:05
 * @LastEditors: gaoluo
 * @LastEditTime: 2021-11-11 16:41:37
 * @FilePath: /nhw/src/components/Echarts/EchartsBarLine/index.vue
-->
<template>
  <div class="echarts-bar-line-container">
    <div ref="container" v-show="tabName.length > 0" class="canvas-wrap"></div>
    <div class="error-msg" v-show="tabName.length <= 0">暂无数据</div>
  </div>
</template>

<script>
import echarts from "../index.js";
import { BarChart, LineChart } from "echarts/charts";
echarts.use([BarChart, LineChart]);

export default {
  name: "EchartsBarLine",
  components: {},
  props: {
    data: {
      type: Object,
      required: true,
    },
    tabName: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      BarLineChart: null,
    };
  },
  watch: {
    data() {
      this.renderCharts();
    },
  },
  computed: {
    bar1Data() {
      return this.data.bar1Data;
    },
    bar2Data() {
      return this.data.bar2Data;
    },
    lineData() {
      return this.data.lineData;
    },
  },
  mounted() {
    const dom = this.$refs.container;
    this.BarLineChart = echarts.init(dom);
    this.renderCharts();
  },
  methods: {
    renderCharts() {
      const color1 = {
        color: new echarts.graphic.LinearGradient(
          0,
          1,
          0,
          0,
          [
            {
              offset: 0,
              color: "#CCCDD1",
            },
            {
              offset: 1,
              color: "#B5B8C1",
            },
          ],
          false
        ),
      };
      const color2 = {
        color: new echarts.graphic.LinearGradient(
          0,
          1,
          0,
          0,
          [
            {
              offset: 0,
              color: "#F6CE7F",
            },
            {
              offset: 1,
              color: "#C5A870",
            },
          ],
          false
        ),
      };
      this.BarLineChart.setOption({
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            crossStyle: {
              color: "#999",
            },
          },
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          top: "20%",
          containLabel: true,
        },
        legend: {
          data: ["发现数", "整改数", "整改率"],
          top: 7,
        },
        xAxis: [
          {
            type: "category",
            data: this.tabName,
            axisPointer: {
              type: "shadow",
            },
            axisLabel: {
              fontSize: 12,
              interval: 0,
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            name: "单位: 个",
            axisLabel: {
              formatter: "{value}",
              fontSize: 12,
            },
            nameTextStyle: {
              fontSize: 12,
              lineHeight: 12,
            },
          },
          {
            type: "value",
            axisLabel: {
              formatter: "{value}%",
            },
          },
        ],
        series: [
          {
            name: "发现数",
            type: "bar",
            data: this.bar1Data,
            itemStyle: color1,
          },
          {
            name: "整改数",
            type: "bar",
            data: this.bar2Data,
            itemStyle: color2,
          },
          {
            name: "整改率",
            type: "line",
            yAxisIndex: 1,
            color: "#E0680F",
            data: this.lineData,
          },
        ],
      });
      this.$nextTick(() => {
        this.BarLineChart.resize();
      });
    },
  },
};
</script>

<style lang="less" scoped>
.echarts-bar-line-container {
  width: 100%;
  .canvas-wrap {
    width: 100%;
    height: 400px;
  }
}
.error-msg {
  text-align: center;
  line-height: 1.9;
  color: #ccc;
}
</style>